package com.ewzj.bigdata.mvp.ui.activity

import android.animation.IntEvaluator
import android.animation.ValueAnimator
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.view.ViewPager
import android.text.TextUtils
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.extensions.dpToPx
import com.ewzj.bigdata.di.component.DaggerDecorateComponent
import com.ewzj.bigdata.di.module.DecorateModule
import com.ewzj.bigdata.mvp.contract.DecorateContract
import com.ewzj.bigdata.mvp.presenter.DecoratePresenter
import com.jakewharton.rxbinding2.view.RxView
import com.jakewharton.rxbinding2.widget.RxTextView
import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.DeviceUtils
import com.jess.arms.utils.UiUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_decorate.*

class DecorateActivity : BaseActivity<DecoratePresenter>(), DecorateContract.View {
    var type :Int=0
    override fun initView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_decorate
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        DaggerDecorateComponent.builder().appComponent(appComponent)
                .decorateModule(DecorateModule(this))
                .build()
                .inject(this)
    }

    override fun initData(savedInstanceState: Bundle?) {
        type=intent.getIntExtra(AppKey.EXTRA_DATA, 0)
        mPresenter.initPagerSlide(vp_content,pagertab)
        pagertab.setOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
               type=position
            }

        })
        when(type){
            1->vp_content.currentItem=1
            0->vp_content.currentItem=0
        }
        initListener()
    }


    override fun showLoading() {

    }

    override fun launchActivity(intent: Intent?) {

    }

    override fun hideLoading() {

    }

    override fun killMyself() {

    }

    override fun showMessage(message: String?) {

    }

    fun showSerachBar(startValue: Int, endValue: Int) {
        //      RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) rlSearch.getLayoutParams();
        val valueAnimator = ValueAnimator.ofInt(0, 100)
        valueAnimator.duration = 200
        valueAnimator.addUpdateListener { animation ->
            val animatorValue = animation.animatedValue as Int
            val fraction = animatorValue / 100f
            val mEvaluator = IntEvaluator()
            //3.使用IntEvaluator计算属性值并赋值给ListView的高
            val layoutParams = rl_search.getLayoutParams()
            layoutParams.width = mEvaluator.evaluate(fraction, startValue, endValue)!!
            rl_search.setLayoutParams(layoutParams)
            rl_search.requestLayout()
        }
        valueAnimator.setTarget(rl_search)
        valueAnimator.start()
    }
    private fun initListener() {
        RxView.clicks(iv_back).subscribe(Consumer { finish() })
        RxView.clicks(iv_search).subscribe(Consumer { showSerachBar(0, UiUtils.getScreenWidth(this) -60.dpToPx(this)) })
        RxView.clicks(tv_cancel).subscribe(Consumer { showSerachBar(UiUtils.getScreenWidth(this) -60.dpToPx(this),0) })
        RxView.clicks(iv_searchhide).subscribe(Consumer {
            if (TextUtils.isEmpty(et_search.getText())) {
                UiUtils.SnackbarText("搜索内容不能为空")
            } else {
                showSerachBar(UiUtils.getScreenWidth(this) -60.dpToPx(this),0)
                DeviceUtils.hideSoftKeyboard(this,et_search)
                mPresenter.searchData(type,et_search.text.toString())
            } })
        RxTextView.editorActionEvents(et_search).observeOn(AndroidSchedulers.mainThread()).subscribe({
            showSerachBar(UiUtils.getScreenWidth(this) -60.dpToPx(this),0)
            DeviceUtils.hideSoftKeyboard(this,et_search)
            mPresenter.searchData(type,et_search.text.toString())
        })
    }
    companion object{
        fun startDecorateActivity(context: Context,type:Int){
            var intent=Intent(context,DecorateActivity::class.java)
            intent.putExtra(AppKey.EXTRA_DATA,type)
            context.startActivity(intent)
        }
    }

    override fun useFragment(): Boolean {
        return true
    }
}
